#Include <..\include\Common_For_PageManager.au3>
#Include <GuiButton.au3>

AutoItSetOption("MustDeclareVars", 1)   ;Variables must be pre-declared.

Dim Const $sCorrectResultPath = "C:\Backup and Restore\Correct Resut\"  
Dim Const $sTestResultPath = "C:\Backup and Restore\Perform Result\"  
Dim Const $sSampleFolderPath = "C:\Backup and Restore\Sample\" 
Dim Const $sTestFolderPath = "C:\Backup and Restore\Test\" 
Dim Const $sRestoreFileFolderPath = "C:\Backup and Restore\Restore File\" 
Dim Const $iNumber = Common_GetNumberInExcelSheet($oExcel,$sBackupRestoreFunctionName)  
Dim Const $iCurrentSheetTotalLineNumber = $iNumber[0]   ;Get all number of rows in current excel sheet.
Dim Const $sFunctionFolderPath = "C:\Backup and Restore\"  
Dim Const $CommonWindowCtrlInfro = ''
;Dim Const $CommonWindowCtrlInfro = '[Class:#32770]'
Dim Const $BackupDialogTitle = "Backup Settings"
Dim Const $BrowseFolderDialogTitle = "Browse For Folder"

Dim Const $sEditMenuHotKey = ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'EditMenuHotKey')
Dim Const $sToolsMenuHotKey = ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'ToolsMenu')
Dim Const $sViewMenuHotKey = ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'ViewMenuHotKey')
Dim Const $sFileMenuHotKey = ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'FileMenuHotKey')
Dim Const $BackupMenu = ReadIniKeyValue($sPCodeIniPath, $sSectionBackupRestore, 'BackupMenu')
Dim Const $RestoreMenu = ReadIniKeyValue($sPCodeIniPath, $sSectionBackupRestore, 'RestoreMenu')
Dim Const $sAnnotationBarCtrlInfo = ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'AnnotationBar')
Dim Const $sExcelSheetName = 'Backup and Restore'
Dim Const $iRescolumn = 14
Global $sCusMsg = ''
Global $sFailLogMsg = ''
Func SetPassword($firstInput,$secondInput)
	Local $password = "Password" 
	ControlClick($CommonWindowCtrlInfro, "", 1044)
	Sleep(800)
	ControlFocus($CommonWindowCtrlInfro, "", 1045)
	ControlSetText($CommonWindowCtrlInfro, "", 1045,$firstInput)
	Sleep(300)
	ControlFocus($CommonWindowCtrlInfro, "", 1046)
	ControlSetText($CommonWindowCtrlInfro, "", 1046,$secondInput)	
	Sleep(300)
	ControlClick($CommonWindowCtrlInfro, "", 1)
	Sleep(500)
EndFunc


Func AddAuthor($Name)
	ControlClick($CommonWindowCtrlInfro, "", 1031)   
	WinWaitActive($CommonWindowCtrlInfro, "", 5)
	Common_SleepTime(1)
	ControlFocus($CommonWindowCtrlInfro, "", "Edit1")
	ControlSetText($CommonWindowCtrlInfro, "", "Edit1", $Name)   ; Set the file name.
	Common_SleepTime(1)
	ControlClick($CommonWindowCtrlInfro, "", 1056)   ; Click add button.
	ControlClick($CommonWindowCtrlInfro, "", 1)   ; Click OK button.
	Common_SleepTime(1)
EndFunc

Func AddKeyword($s)
	ControlClick($CommonWindowCtrlInfro, "", 1032)   
	WinWaitActive($CommonWindowCtrlInfro, "", 5)
	Common_SleepTime(1)
	ControlFocus($CommonWindowCtrlInfro, "", "Edit1")
	ControlSetText($CommonWindowCtrlInfro, "", "Edit1", $s)   ; Set the file name.
	Common_SleepTime(1)
	ControlClick($CommonWindowCtrlInfro, "", 1056)   ; Click add button.
	ControlClick($CommonWindowCtrlInfro, "", 1)   ; Click OK button.
	Common_SleepTime(1)
EndFunc

Func SwitchToSampleFolder($hPMHandle)
	Common_SleepTime(2)	
	Local $hTree = ControlGetHandle($hPMHandle, "", $sTreeViewCtrInfo)
	Local $hItemFound1 = _GUICtrlTreeView_FindItem($hTree, "Sample")
	Common_SleepTime(1)
	_GUICtrlTreeView_ClickItem($hTree, $hItemFound1)	
	_GUICtrlTreeView_SelectItem($hTree, $hItemFound1)
	Common_SleepTime(4)	
	WinActivate($hPMHandle)
EndFunc

Func SwitchToTestFolder($hPMHandle)
	Local $hTree = ControlGetHandle($hPMHandle, "", $sTreeViewCtrInfo)
	Local $hItemFound1 = _GUICtrlTreeView_FindItem($hTree, "Test")
	_GUICtrlTreeView_ClickItem($hTree, $hItemFound1)	
	_GUICtrlTreeView_SelectItem($hTree, $hItemFound1)
	Sleep(300)
	Common_SleepTime(2)	
	WinActivate($hPMHandle)
EndFunc

Func SwitchToRestoreFolder($hPMHandle)
	Local $hTree = ControlGetHandle($hPMHandle, "", $sTreeViewCtrInfo)
	Local $hItemFound1 = _GUICtrlTreeView_FindItem($hTree, "Restore File")
	_GUICtrlTreeView_ClickItem($hTree, $hItemFound1)	
	Sleep(300)
	Common_SleepTime(2)	
	WinActivate($hPMHandle)
EndFunc

Func SwitchToRestoreFolder_($hPMHandle)
	Local $hTree = ControlGetHandle($hPMHandle, "", $sTreeViewCtrInfo)
	Local $hItemFound1 = _GUICtrlTreeView_FindItem($hTree, "Restore File")
	_GUICtrlTreeView_Expand($hTree,$hItemFound1)	
	Sleep(300)
	Local $hItemFound2 = _GUICtrlTreeView_FindItem($hTree, "Sample")
	_GUICtrlTreeView_ClickItem($hTree, $hItemFound2)
	Common_SleepTime(6)	
	WinActivate($hPMHandle)
EndFunc

Func SelectRestoreSource($TestCaseID)
	ControlClick($CommonWindowCtrlInfro, "", 1014)
	Common_SleepTime(8)		
	Local $hTree = ControlGetHandle($CommonWindowCtrlInfro, "", 14145)
	Local $hItemFound1 = _GUICtrlTreeView_FindItem($hTree, @UserName)
	_GUICtrlTreeView_Expand($hTree,$hItemFound1,False)
	Sleep(300)
	Local $hItemFound2 = _GUICtrlTreeView_FindItem($hTree, 'Computer')
	_GUICtrlTreeView_Expand($hTree,$hItemFound2)	
	Sleep(300)
	Local $hItemFound3 = _GUICtrlTreeView_FindItem($hTree, $sDiskC_DiskVolumeName)
	_GUICtrlTreeView_Expand($hTree,$hItemFound3)	
	Sleep(300)
	Local $hItemFound4 = _GUICtrlTreeView_FindItem($hTree, $sSectionBackupRestore)
	_GUICtrlTreeView_Expand($hTree, $hItemFound4)	
	Sleep(300)
	Local $hItemFound5 = _GUICtrlTreeView_FindItem($hTree, 'Perform Result')
	_GUICtrlTreeView_Expand($hTree, $hItemFound5)	
	Sleep(300)
	Local $hItemFound6 = _GUICtrlTreeView_FindItem($hTree, $TestCaseID)	
	_GUICtrlTreeView_Expand($hTree, $hItemFound6)
	Sleep(300)
	_GUICtrlTreeView_ClickItem($hTree, $hItemFound6)	
	Sleep(300)
	ControlClick($CommonWindowCtrlInfro,'',1)
	Common_SleepTime(2)

EndFunc

Func SetBackupDestination()
	ControlClick($CommonWindowCtrlInfro, "", 1022)
	If WinWaitActive($BrowseFolderDialogTitle, '', 10) Then
		Common_SleepTime(2)	
		Local $hTree = ControlGetHandle($CommonWindowCtrlInfro, "", 100)
		Local $hItemFound1 = _GUICtrlTreeView_FindItem($hTree, @UserName)
		_GUICtrlTreeView_Expand($hTree,$hItemFound1,False)
		Sleep(300)
		Local $hItemFound2 = _GUICtrlTreeView_FindItem($hTree, 'Computer')
		_GUICtrlTreeView_Expand($hTree,$hItemFound2)	
		Sleep(300)
		Local $hItemFound3 = _GUICtrlTreeView_FindItem($hTree, $sDiskC_DiskVolumeName)
		_GUICtrlTreeView_Expand($hTree,$hItemFound3)	
		Sleep(300)
		Local $hItemFound4 = _GUICtrlTreeView_FindItem($hTree, $sSectionBackupRestore)
		_GUICtrlTreeView_Expand($hTree, $hItemFound4)	
		Sleep(300)
		Local $hItemFound5 = _GUICtrlTreeView_FindItem($hTree, 'Perform Result')
		_GUICtrlTreeView_ClickItem($hTree, $hItemFound5)	
		_GUICtrlTreeView_SelectItem($hTree, $hItemFound5)
		Sleep(300)
		ControlClick($BrowseFolderDialogTitle,'',1)
		Common_SleepTime(2)
		Return True
	Else
		Return False
	EndIf
EndFunc

Func SetBuckupLabel($sName)
	ControlSetText($CommonWindowCtrlInfro, "", 1008,$sName)
	Sleep(300)
EndFunc

Func SetBuckupName($sName)
	ControlSetText($CommonWindowCtrlInfro, "", 1010,$sName)
	Sleep(300)
EndFunc

Func ClickOKOnBackupSettings()
	ControlClick($CommonWindowCtrlInfro, "", 1)   ; OK Button.
	Common_SleepTime(6)
	WinWaitActive($CommonWindowCtrlInfro, "Backup complete.", 10)
	Sleep(800)
	Send("{ENTER}")
	Sleep(500)	
EndFunc

Func ClickOKOnRestoreSettings()
	ControlClick($CommonWindowCtrlInfro, "", 1)   ; OK Button.
	Common_SleepTime(6)
	WinWaitActive($CommonWindowCtrlInfro, "Restore complete.", 10)
	Sleep(800)
	Send("{ENTER}")
	Sleep(500)	
EndFunc

Func OpenBackupSettingDlg()
	OpenMenu("File")
	Send($BackupMenu)
	Common_SleepTime(2)
	WinWaitActive($CommonWindowCtrlInfro, "", 5)
EndFunc

Func OpenRestoreSettingDlg()
	OpenMenu("File")
	Send($RestoreMenu)
	Common_SleepTime(2)
	WinWaitActive($CommonWindowCtrlInfro, "", 5)
EndFunc

Func SetFileReadonly()
	Local $hWnd = ControlGetHandle($CommonWindowCtrlInfro, "", 1030)
	If _GUICtrlButton_GetCheck($hWnd) = 0 Then
		ControlClick($CommonWindowCtrlInfro, "", 1030)
		Sleep(300)
	EndIf
EndFunc

Func InitializeStatusForBackupAndRestore()
	Local $FileList = _FileListToArray ($sFunctionFolderPath,"*", 1)
	If Not @error Then
		If $FileList[0] >= 1 Then
			For $i = 1 to $FileList[0]
				FileDelete ($sFunctionFolderPath & $FileList[$i])
			Next
			Return 1
		EndIf
	EndIf
	Local $TheFolder = StringLeft(@MyDocumentsDir, StringInStr(@MyDocumentsDir, "\",0, -1)) & "Sample"
	
	If FileExists($TheFolder) Then
		DirRemove($TheFolder, 1)
	EndIf
	
	FileSetAttrib($sSampleFolderPath & "BMP.bmp","-R")
	FileSetAttrib($sRestoreFileFolderPath & "Sample\BMP.bmp","-R")
	Sleep(300)
	DirRemove($sRestoreFileFolderPath & "Sample" , 1 )
	DirRemove($sTestFolderPath & "Sample", 1 )
EndFunc

Func OpenMenu($sMenu)
	Switch $sMenu
	Case "Edit"
		Send("!" & $sEditMenuHotKey)	
		Common_SleepTime(2)	
	Case "Tools"
		Send("!" & $sToolsMenuHotKey)	
		Common_SleepTime(2)	
	Case "View"
		Send("!" & $sViewMenuHotKey)	
		Common_SleepTime(2)	
	Case "File"
		Send("!" & $sFileMenuHotKey)	
		Common_SleepTime(2)
	EndSwitch	
EndFunc
































